package com.jsb.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.jsb.service.AnswerService;
import com.jsb.service.CompanyService;
import com.jsb.service.QuestionService;
import com.jsb.service.QuestionUserService;
import com.jsb.service.RedisService;
import com.jsb.service.UserService;
import com.jsb.common.utils.StringUtils;
import com.jsb.dto.UserCompany;
import com.jsb.model.AnswerExample;
import com.jsb.model.Company;
import com.jsb.model.QuestionExample;
import com.jsb.model.QuestionUserExample;
import com.jsb.model.User;
import com.jsb.model.UserExample;
import com.jsb.model.UserExample.Criteria;

@Controller
public class UserController{
	
	private int defaultPageSize=10;
	
	private String redirect = "redirect:/user/list.do";
	
	@Resource
	private UserService userService;
	
	@Resource
	private CompanyService companyService;
	
	@Resource
	private QuestionService questionService;
	@Resource
	private QuestionUserService questionUserService;
	@Resource
	private AnswerService answerService;
	@Resource
	private RedisService redisService;
	
	@RequestMapping("/user/list")
	public String list(
			Model model,String phone, String role,String realName,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		UserExample userExample = new UserExample();
		Criteria createCriteria = userExample.createCriteria();
		if(StringUtils.isNotBlank(phone)){
			createCriteria.andPhoneLike("%"+phone+"%");
		}
		if(StringUtils.isNotEmpty(role)){
			createCriteria.andRoleEqualTo(Integer.valueOf(role));
		}
		if(StringUtils.isNotEmpty(realName)){
			createCriteria.andRealNameLike("%"+realName+"%");
		}
		int totalCount = userService.countUser(userExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		userExample.setPageIndex((pageNum - 1) * pageSize);
		userExample.setPageSize(pageSize);
		userExample.setOrderByClause("registerDate DESC");
		List<User> users = userService.getPageUser(userExample);
		List<UserCompany> userCompanys = new ArrayList<UserCompany>();
		for (User user : users) {
			UserCompany userCompany = new UserCompany();
			Company companyByUuid = companyService.getCompanyByUuid(user.getCompanyUuid());
			userCompany.setCompany(companyByUuid);
			userCompany.setUser(user);
			
			String phoneNumber = user.getPhoneNumber();
			//从问题表中查询悬赏次数
			QuestionExample questionExample= new QuestionExample();
			questionExample.createCriteria().andPhoneNumEqualTo(phoneNumber);
			int questionCount = questionService.countQuestion(questionExample);
			userCompany.setQuestionCount(questionCount);
			//从回答表中查询抢答次数
			AnswerExample answerExample= new AnswerExample();
			answerExample.createCriteria().andPhoneNumberEqualTo(phoneNumber);
			int answerCount = answerService.countAnswer(answerExample);
			userCompany.setAnswerCount(answerCount);
			//从questionuser表中查询偷看次数
			QuestionUserExample questionUserExample = new QuestionUserExample();
			questionUserExample.createCriteria().andPhoneNumberEqualTo(phoneNumber);
			int questionUserCount = questionUserService.countQuestionUser(questionUserExample);
			userCompany.setQuestionUserCount(questionUserCount);
			
			userCompanys.add(userCompany);
		}
		
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		model.addAttribute("phone", phone);
		model.addAttribute("role", role);
		model.addAttribute("realName", realName);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("userCompanys", userCompanys);
		return "/User/list";
	}
	
	@RequestMapping("/user/toAdd")
	public String toAdd(Model model) {
		return "/User/add";
	}

	@RequestMapping("/user/add")
	public String add(Model model, @ModelAttribute("user") User user) {
		userService.addUser(user);
		return redirect;
	}

	@RequestMapping("/user/del")
	public String del(Model model,
			@RequestParam(value = "phoneNumber", required = true) String phoneNumber) {
		userService.delUserByPhoneNumber(phoneNumber);
		return redirect;
	}

	@RequestMapping("/user/toEdit")
	public String toEdit(Model model,
			@RequestParam(value = "phoneNumber", required = true) String phoneNumber) {
		User user = userService.getUserByPhoneNumber(phoneNumber);
		model.addAttribute("user", user);
		return "/User/edit";
	}

	@RequestMapping("/user/edit")
	public String edit(Model model, @ModelAttribute("user") User user) {
		userService.updateUser(user);
		return redirect;
	}
	
	@RequestMapping("/user/freeze")
	public String freeze(Model model, @ModelAttribute("user") User user) {
		String phoneNumber = user.getPhoneNumber();
		User u = new User();
		u.setPhoneNumber(phoneNumber);
		u.setRole(0);
		userService.updateUser(u);
		Set<String> keys = redisService.keys("C_"+phoneNumber+"*");
		if(null != keys&&keys.size()>0){
			for (String str : keys) {
				redisService.delete(str);
			}
		}
		return redirect;
	}
	//TODO解冻逻辑
	@RequestMapping("/user/unfreeze")
	public String unfreeze(Model model, @ModelAttribute("user") User user) {
		user.setRole(0);
		userService.updateUser(user);
		return redirect;
	}

	@RequestMapping("/user/view")
	public String view(Model model,
			@RequestParam(value = "phoneNumber", required = true) String phoneNumber) {
		User user = userService.getUserByPhoneNumber(phoneNumber);
		model.addAttribute("user", user);
		return "/User/view";
	}
}
